package com.core.website.api;

import com.core.website.common.Result;
import com.core.website.common.ResultUtil;
import com.core.website.common.TableDataInfo;
import com.core.website.entity.Column;
import com.core.website.entity.IpEntity;
import com.core.website.mapper.AccessMapper;
import com.core.website.service.AccessService;
import com.core.website.service.ColumnService;
import com.core.website.service.IpService;
import com.core.website.util.IpAddressUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ipApi")
public class IpApi {

    @Autowired
    private IpService ipService;
    @Autowired
    private AccessService accessService;
    @Autowired
    private ColumnService columnService;
    //添加
    @PostMapping("/add")
    public Result add(@RequestBody IpEntity ipEntity){
        ipService.addIp(ipEntity);
        return ResultUtil.success();
    }
    @PostMapping("/update")
    public Result update(@RequestBody IpEntity ipEntity){
        ipService.update(ipEntity);
        return ResultUtil.success();
    }
    //分页查询
    @PostMapping("/queryPage")
    public TableDataInfo queryCascadePage(@RequestBody IpEntity ipEntity){
        PageHelper.startPage(ipEntity.getPageNum(), ipEntity.getPageSize());
        List<IpEntity> result=ipService.queryPage(ipEntity.getColumnId(),ipEntity.getIp());
        return TableDataInfo.getDataTable(result);
    }
    //删除
    @PostMapping("/delete")
    public Result delete(@RequestBody List<String> ipList){
        ipService.delete(ipList);
        return ResultUtil.success();
    }
    //查询ip是否可以访问
    @GetMapping("/queryFilterColumn")
    public Result queryFilterColumn( HttpServletRequest request){
        accessService.addDayAccess();
        List<Column> resultColumn=new ArrayList<>();
        //返回受限的栏目
        String ipAddress = IpAddressUtil.getIpAddr(request);
        Set<String> columnIdSet=new HashSet<>();
        if(StringUtils.isNotEmpty(ipAddress)){
            if(ipAddress.contains(",")){
                String[] split = ipAddress.split(",");
                for(String ip:split){
                    ipService.addStatisticalIpNum(ip);
                    Set<String> result= ipService.queryFilterColumn(ip.trim());
                    columnIdSet.addAll(result);
                }
            }else{
                ipService.addStatisticalIpNum(ipAddress.trim());
                Set<String> result= ipService.queryFilterColumn(ipAddress.trim());
                columnIdSet.addAll(result);
            }
        }
        if(columnIdSet.size()>0){
            for(String id:columnIdSet){
                if(StringUtils.isNotEmpty(id)){
                    Column column = columnService.queryOne(id);
                    if(null!=column){
                        resultColumn.add(column);
                    }
                }
            }
        }

        return ResultUtil.success(resultColumn);
    }
    @PostMapping("/statisticalIpSetting")
    public Result statisticalIpSetting(@RequestBody List<String> ipList){
        ipService.statisticalIpSetting(ipList);
        return ResultUtil.success(1);
    }
    @GetMapping("/statisticalIpSearch")
    public Result statisticalIpSearch(){
        List<String> result= ipService.statisticalIpSearch();
        return ResultUtil.success(result);
    }
    @GetMapping("/statisticalIpTotal")
    public Result statisticalIpTotal(){
        int i=ipService.statisticalIpTotal();
        return ResultUtil.success(i);
    }
}
